package com.xiaocq.wordskiller;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Statistic extends Activity {
	private Button backToMenu, clearData;
	private TextView showAllTestCounts, showAllRightCounts, showAllWrongCounts;
	private TextView showAllHitRate;
	private TextView showAllTimeCost;

	private int allTestCounts, allRightCounts, allWrongCounts;
	private double allHitRate;
	private long allTimeCostInMillis;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistic);

		prepareDataFromPreference();

		initComponents();
	}

	private void prepareDataFromPreference() {
		SharedPreferences pref = getSharedPreferences(
				CommandList.STATISTIC_DATA, MODE_PRIVATE);

		allTestCounts = pref.getInt(CommandList.ALL_TEST_COUNTS, 0);
		allWrongCounts = pref.getInt(CommandList.ALL_WRONG_COUNTS, 0);
		allTimeCostInMillis = pref.getLong(CommandList.ALL_TIME_COST, 0L);
	}

	private void initComponents() {
		backToMenu = (Button) findViewById(R.id.statistic_backToMenu);
		clearData = (Button) findViewById(R.id.statistic_clear);
		showAllTestCounts = (TextView) findViewById(R.id.statistic_allTestCounts);
		showAllRightCounts = (TextView) findViewById(R.id.statistic_allRightCounts);
		showAllWrongCounts = (TextView) findViewById(R.id.statistic_allWrongCounts);
		showAllHitRate = (TextView) findViewById(R.id.statistic_allHitRate);
		showAllTimeCost = (TextView) findViewById(R.id.statistic_allTimeCost);

		backToMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

		clearData.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new AlertDialog.Builder(Statistic.this)
						.setMessage(R.string.tip_clearData)
						.setPositiveButton(android.R.string.ok,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										clearAllData();
										setTextOfComponents();
									}
								})
						.setNegativeButton(android.R.string.cancel,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
									}
								}).show();
			}
		});
		
		setTextOfComponents();
	}

	private void clearAllData() {
		SharedPreferences pref = getSharedPreferences(
				CommandList.STATISTIC_DATA, MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(CommandList.ALL_TEST_COUNTS, 0);
		editor.putInt(CommandList.ALL_WRONG_COUNTS, 0);
		editor.putLong(CommandList.ALL_TIME_COST, 0L);
		editor.commit();

		allTimeCostInMillis = 0L;
		allTestCounts = 0;
		allWrongCounts = 0;
	}

	private void setTextOfComponents() {
		allRightCounts = allTestCounts - allWrongCounts;
		allHitRate = allRightCounts * 1.0 / allTestCounts;

		showAllTestCounts.setText("已测试：" + allTestCounts + "个单词");
		showAllRightCounts.setText("已答对：" + allRightCounts + "个单词");
		showAllWrongCounts.setText("已答错：" + allWrongCounts + "个单词");
		showAllHitRate.setText("命中率：" + (allRightCounts == 0 ? "0.00" : String.format("%1$-4.2f", allHitRate)) + "%");

		showAllTimeCost.setText(getFormatData(allTimeCostInMillis));
	}

	private String getFormatData(long timeCost) {
		long second = timeCost / 1000L;
		long minute = second / 60L;
		second %= 60L;
		long hour = minute / 60L;
		minute %= 60L;
		long day = hour / 24L;
		hour %= 24;

		return (day > 0L ? day + "天 " : "") 
				+ (hour > 0L ? hour + "小时 " : "")
				+ (minute > 0L ? minute + "分钟 " : "")
				+ second + "秒";
	}
}
